import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;

public class MapTest1 {
    public static void main(String[] args) {
        // Map<键, 值> 键是唯一的，值可以重复
        Map<Integer, String> map = new HashMap<Integer, String>();
        // put() 将指定的值与此映射中的指定键关联
        map.put(1, "张三");
        map.put(2, "李四");
        map.put(3, "王五");
        System.out.println(map);// {1=张三, 2=李四, 3=王五}

        System.out.println("---------遍历key-------");
        Set<Integer> keys = map.keySet();
        for (Integer key : keys) {
            System.out.println(key);
        }

        System.out.println("-----遍历value值-------");
        Collection<String> values = map.values();
        values.forEach((items) -> {
            System.out.println(items);
        });

        System.out.println("------遍历键值对-------");
        keys = map.keySet();
        for (Integer key : keys) {
            System.out.println(key + "=" + map.get(key));
        }

        System.out.println("------遍历键值对-------");
        Set<Map.Entry<Integer, String>> set = map.entrySet();
        for (Map.Entry<Integer, String> item : set) {
            System.out.println(item.getKey() + "=" + item.getValue());
        }

        System.out.println("------遍历键值对-------");
        map.forEach(new BiConsumer<Integer, String>() {
            @Override
            public void accept(Integer i, String s) {
                System.out.println(i + "=" + s);
            }
        });
        map.forEach((key, value) -> {
            System.out.println(key + "=" + value);
        });

    }
}
